$(document).ready(function() {
	$('.fancybox').fancybox();
	if(typeof startDate !== 'undefined'){
		if (new Date().getTime() <= new Date(startDate).getTime()) {
			$.fancybox.open([ {
				href : '#form_finish',
				closeBtn : false,
				closeClick : false,
				parent: "body:first",
				keys : {
					close : null
				},
				helpers : {
					overlay : {
						closeClick : false
					}
				}
			} ], {
				padding : 20
			});
		}
	}
	else if(typeof endDate !== 'undefined'){
		if (new Date().getTime() >= new Date(endDate).getTime()) {
			$.fancybox.open([ {
				href : '#form_finish',
				closeBtn : false,
				closeClick : false,
				parent: "body:first",
				keys : {
					close : null
				},
				helpers : {
					overlay : {
						closeClick : false,
						parent: $('body')
					}
				}
			} ], {
				padding : 20
			});
		}
	}
	if(typeof notSend !== 'undefined'&&notSend==1){
		$.fancybox.open([ {
			href : '#form_finish',
			closeBtn : false,
			closeClick : false,
			parent: "body:first",
			keys : {
				close : null
			},
			helpers : {
				overlay : {
					closeClick : false
				}
			}
		} ], {
			padding : 20
		});
		setTimeout('$("#submitFormBtn").attr("disabled","disabled");',100);
	}
	
//	if($('#formPreview').hasClass('newform')){
//		$.ajax({
//			  url: "/app/protected/modules/webforms/includes/js/intlTelInput.js",
//			  dataType: "script",
//			  success: function(response){			  
//						  var telInput = $('.mobile, .confirm-mobile');
//							telInput.parent().parent().addClass('intl-tel-div');
//							telInput.parent().addClass('intl-tel-div');
//							telInput.intlTelInput({
//								preferredCountries : [ 'hk', 'cn', 'mo' ],
//								nationalMode : false,
//								autoHideDialCode : false,
//								geoIpLookup : function(callback) {
//									$.get('https://api.ipinfodb.com/v3/ip-country/?key=8bb84e64f70bbd74e72d07b48eaab1100de35c5cf49eb5c93091cef0eaadb97c&format=json', function() {
//									}, 'json')
//									.always(
//											function(resp) {
//										var countryCode = (resp && resp.countryCode) ? resp.countryCode : '';
//										callback(countryCode);
//									});
//								},
//								utilsScript : 'https://webform.xgate.com/app/protected/modules/webforms/includes/js/utils.js'
//							});
//					    }
//			        
//			});
//	}
	
	if($('.field.radio, .field.checkbox').hasClass('new-style')){
		$.ajax({
			  url: "/app/protected/modules/webforms/includes/js/icheck.js?v=1.0.2",
			  dataType: "script",
			  success: function(response){
			        $('.field.radio, .field.checkbox').iCheck({
						checkboxClass: 'icheckbox_square', radioClass: 'iradio_square',increaseArea: '20%'
					});
			    }
			});
	}
});

function updatePageTitle(title){
	document.title = title;
}

function fancyconfirm(msg, options, callback) {
    $.fancybox("#redemptionConfirmBox", {
        modal: options.modal,
        beforeShow: function () {
            this.content.prepend("<p class=\"title\"></p>");
            $(".title").html(msg);

            if (options.buttons == null) {
                options.buttons = [{
                    clsName: "yes",
                    title: "確認",
                    value: true
                }, {
                    clsName: "no",
                    title: "取消",
                    value: false
                }];
            }

            for (i = 0; i < options.buttons.length; i++) {
                this.content.append($("<input>", {
                    type: "button",
                    'class': "confirm " + options.buttons[i]['class'],
                    value: options.buttons[i].title
                }).data("index", i).css("margin-left", ((i > 0) ? "10px" : "")));
            }
            $(this.content).css("text-align", "center");
        },
        afterShow: function () {
            $(".confirm").on("click", function (event) {
                ret = options.buttons[$(event.target).data("index")].value;
                $.fancybox.close();
            });
        },
        afterClose: function () {
        	$('.title').remove();
        	$('.yes').remove();
        	$('.no').remove();
            callback.call(this, ret);
            $("#submitFormBtn").removeAttr("disabled");
        }
    });
}

function redeemCode(){
	var tracker = '';
	if (typeof globalTracker!=='undefined'){
		tracker = globalTracker;
	}
	if(getParameterByName('tracker')!=''){
		tracker = getParameterByName('tracker');
	}
	var encid = getParameterByName('encid');
	var code = getParameterByName('code');
	var url = hostname+'/app/index.php/webforms/form/ajaxredeemcode?identifier='+identifier+'&encid='+encid+'&tracker='+tracker+'&code='+code;
	var response = -1;
	$.ajax({ 
	    type: 'GET',
	    url: url ,
	    async:false,
	    success: function(result){
	    	if(result==1){
	    		response = result;
	    	}
	    },
	});
	return response;
}
